﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Waaahsabi.Controls.FileSystem
{
	public class GatewayFileItem : Control, IFileItem
	{

		#region FileName DependencyProperty
		public static readonly DependencyProperty FileNameProperty =
		DependencyProperty.Register
			(
			"FileName",
			typeof(string),
			typeof(GatewayFileItem),
			new PropertyMetadata("No filename set")
			);

		public string FileName
		{
			get { return (string)GetValue(FileNameProperty); }
			set { SetValue(FileNameProperty, value); }
		}
		#endregion

		#region FileCompressionLevel DependencyProperty
		public static readonly DependencyProperty FileCompressionLevelProperty =
		DependencyProperty.Register
			(
			"FileCompressionLevel",
			typeof(double),
			typeof(GatewayFileItem),
			new PropertyMetadata(0.0)
			);

		public double FileCompressionLevel
		{
			get { return (double)GetValue(FileCompressionLevelProperty); }
			set { SetValue(FileCompressionLevelProperty, value); }
		}
		#endregion

		#region FileEncryptionLevel DependencyProperty
		public static readonly DependencyProperty FileEncryptionLevelProperty =
		DependencyProperty.Register
			(
			"FileEncryptionLevel",
			typeof(double),
			typeof(GatewayFileItem),
			new PropertyMetadata(0.0)
			);

		public double FileEncryptionLevel
		{
			get { return (double)GetValue(FileEncryptionLevelProperty); }
			set { SetValue(FileEncryptionLevelProperty, value); }
		}
		#endregion

		#region FileSize DependencyProperty
		public static readonly DependencyProperty FileSizeProperty =
		DependencyProperty.Register
			(
			"FileSize",
			typeof(int),
			typeof(GatewayFileItem),
			new PropertyMetadata(null)
			);

		public int FileSize
		{
			get { return (int)GetValue(FileSizeProperty); }
			set { SetValue(FileSizeProperty, value); }
		}
		#endregion

		#region FileVersion DependencyProperty
		public static readonly DependencyProperty FileVersionProperty =
		DependencyProperty.Register
			(
			"FileVersion",
			typeof(int),
			typeof(GatewayFileItem),
			new PropertyMetadata(null)
			);

		public int FileVersion
		{
			get { return (int)GetValue(FileVersionProperty); }
			set { SetValue(FileVersionProperty, value); }
		}
		#endregion

		#region Selected DependencyProperty
		public static readonly DependencyProperty SelectedProperty =
		DependencyProperty.Register
			(
			"Selected",
			typeof(bool),
			typeof(GatewayFileItem),
			new PropertyMetadata(null)
			);

		public bool Selected
		{
			get { return (bool)GetValue(SelectedProperty); }
			set { SetValue(SelectedProperty, value); }
		}
		#endregion

		#region Mode DependencyProperty
		public static readonly DependencyProperty ModeProperty =
		DependencyProperty.Register
			(
			"Mode",
			typeof(FileItemDisplayMode),
			typeof(GatewayFileItem),
			new PropertyMetadata(FileItemDisplayMode.Large, (s,e) =>
			{
				if(s is GatewayFileItem)
				{
					var fi = s as GatewayFileItem;
					string state;
					if (((FileItemDisplayMode)e.NewValue) == FileItemDisplayMode.Condensed)
						state = "Condensed";
					else
						state = "Large";
					VisualStateManager.GoToState(fi, state, false);
				}
			})
			);

		public FileItemDisplayMode Mode
		{
			get { return (FileItemDisplayMode)GetValue(ModeProperty); }
			set { SetValue(ModeProperty, value); }
		}
		#endregion


		public event EventHandler OnFileSizeChanged; //this is for updating the item height upon compression etc.

		public ulong FileId
		{
			get; set;
		}

		public event EventHandler Click;
		private bool _isSelected;
		public bool IsSelected { get { return _isSelected; } }

		public GatewayFileItem()
		{
			DefaultStyleKey = typeof(GatewayFileItem);
		}

		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			if (Mode == FileItemDisplayMode.Condensed)
				VisualStateManager.GoToState(this, "Condensed", false);
		}

		protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
		{
			if (!_isSelected)
				_isSelected = true;
			if (Click != null)
				Click(this, e);
			base.OnMouseLeftButtonUp(e);
		}

		protected override void OnMouseEnter(MouseEventArgs e)
		{
			if (_isSelected) return;
			VisualStateManager.GoToState(this, "MouseOver", true);
			base.OnMouseEnter(e);
		}

		protected override void OnMouseLeave(MouseEventArgs e)
		{
			if (_isSelected) return;
			VisualStateManager.GoToState(this, "Idle", true);
			base.OnMouseLeave(e);
		}
	}

	public enum FileItemDisplayMode
	{
		Large,
		Condensed
	}
}
